"""
1.等待设置.py
    1.强制等待
        sleep(秒)
      脚本调试  , 教学  , 工作项目
    2.隐式等待  最简单
        driver = webdriver.Chrome()
        driver.implicitly_wait( 超时时间)  10
        # 等待整个页面加载完成, 超出规定的时间 , 抛出异常.
        4.0
    3.显式等待
        在调用的每个元素之前,进行显式的等待操作
        导入模块:
        from selenium.webdriver.support.wait import WebDriverWait
        from selenium.webdriver.support import expected_conditions
        1.expected_conditions.presence_of_element_located( (元素定位方式))
            判断符合元素定位方法的元素已经出现.
        2.expected_conditions.frame_to_be_available_and_switch_to_it("iframe-superadmin")
            判断框架是否出现并切入.
        3.expected_conditions.text_to_be_present_in_element() #
            元素的.text 属性    获取元素的文本信息 ,超连接
            EC.text_to_be_present_in_element( (By.CLASS_NAME, "foo"),  "bar"))
            判断的文本值是否出现
        4.expected_conditions.element_to_be_clickable()
            判断元素可以被点击
            element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, "foo"))
        5.expected_conditions.text_to_be_present_in_element_value() # 判断超连接的文本值是否出现
            判断元素的value属性中是否出现指定文本.
        6.expected_conditions.alert_is_present() 判断
            判断弹出框是否出现,如果出现则切入弹出框,并返回一个弹窗框对象
            alert = WebDriverWait(browser,10).until(expected_conditions.alert_is_present())
            弹出框操作:
                    # 切入弹出框
                   alert=driver.switch_to.alert()   # 使用显式等待 ,省略 该语句.
                   # 弹出框的操作
                    tips = alert.text  # 获取弹出框的文本信息
                   alert.accept() # 点击是
                   alert.dismiss() # 点击否
                   alert.send_keys() 向弹出框输入值
            print(tips)
# 点击警告窗口中的 确定 按钮
        配合 until方法使用
"""

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.select import Select
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
# driver.minimize_window()
# 隐式等待
# driver.implicitly_wait(10)
driver.maximize_window()
# 判断超连接的文本值是否出现

def login():
    driver.get("http://localhost/ranzhi/www/")
    # 显式等待
    WebDriverWait(driver,10).until(expected_conditions.presence_of_element_located((By.ID,"account")))
    t = driver.find_element(By.ID,"account")
    # #动作链表示可以连续执行的动作,每个动作用 '.'进行连接,最后使用perform()进行结尾
    t.send_keys("admin")
    WebDriverWait(driver, 10).until(expected_conditions.presence_of_element_located((By.ID,"password")))
    driver.find_element(By.ID,"password").send_keys("123456")
    WebDriverWait(driver, 10).until(expected_conditions.presence_of_element_located((By.ID,"submit")))
    driver.find_element(By.ID,"submit").click()
    WebDriverWait(driver, 10).until(expected_conditions.presence_of_element_located((By.XPATH,"/html/body/div[1]/div[1]/div/ul[1]/li[8]/button/i")))
    driver.find_element(By.XPATH,"/html/body/div[1]/div[1]/div/ul[1]/li[8]/button/i").click()
    # 切入框架的显式等待
    WebDriverWait(driver, 10).until(expected_conditions.frame_to_be_available_and_switch_to_it("iframe-superadmin"))
    #driver.switch_to.frame("iframe-superadmin")
    WebDriverWait(driver, 10).until(expected_conditions.presence_of_element_located(
        (By.CSS_SELECTOR,"html.screen-desktop-wide.device-desktop body.m-admin-index div.row.page-content.with-menu div#shortcutBox.container div.row div.col-md-4.col-sm-6 div.shortcut.user a h3")))
    driver.find_element(By.CSS_SELECTOR,"html.screen-desktop-wide.device-desktop body.m-admin-index div.row.page-content.with-menu div#shortcutBox.container div.row div.col-md-4.col-sm-6 div.shortcut.user a h3").click()

    # 添加成员
    # 定位下拉框
    WebDriverWait(driver, 10).until(expected_conditions.presence_of_element_located((By.ID, "role")))
    driver.find_element(By.ID, "role").click()
    WebDriverWait(driver, 10).until(expected_conditions.presence_of_element_located((By.XPATH, "/html/body/div/div/div[2]/div/div[2]/form/table/tbody/tr[5]/td[1]/select/option[9]")))
    driver.find_element(By.XPATH, "/html/body/div/div/div[2]/div/div[2]/form/table/tbody/tr[5]/td[1]/select/option[9]").click()
    # ele = driver.find_element(By.ID, "role")
    # ele.send_keys("技术支持")
    # 生成下拉框对象
    # select_object = Select(ele)
    # 1.通过 select_by_index() 选择下拉框的值.
    # select_object.select_by_index(8)
    # 2.通过 value 值进行选择:
    # select_object.select_by_value("support")
    # 3.通过选项的文本值进行选择
    # select_object.select_by_visible_text("技术支持")
    # 切出 不需加 显式等待
    driver.switch_to.default_content()
    WebDriverWait(driver, 10).until(expected_conditions.presence_of_element_located((By.LINK_TEXT,"签退")))
    driver.find_element(By.LINK_TEXT,"签退").click()
    driver.quit()
if __name__ == '__main__':
    login()